<template>
  <div>
    <h1>拼团活动列表</h1>
    <!-- 条件查询 -->
    <div style="margin-top: 10px">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="活动名称">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="请输入活动名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadGroupActiveList"
            >查询</el-button
          >
          <el-button type="primary" @click="toAddGroupActive">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格数据 -->
    <div style="margin-top: 10px">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="activeId" label="id" width="80" />
        <el-table-column prop="activeName" label="活动名称" width="120" />
        <el-table-column prop="groupNum" label="组团人数" width="100" />
        <el-table-column prop="beginTime" label="开始时间" width="160" />
        <el-table-column prop="endTime" label="截止时间" width="160" />
        <el-table-column label=" 操作">
          <template #default="scope">
            <el-button
              type="info"
              @click="onUpdateActive(scope.row)"
              size="small"
              >编辑</el-button
            >
            <el-button
              type="danger"
              @click="onDeleteActive(scope.row)"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div style="margin-top: 10px">
      <div class="demo-pagination-block">
        <el-pagination
          v-model:current-page="searchForm.pageNum"
          v-model:page-size="searchForm.pageSize"
          :page-sizes="[2, 4, 6, 8]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- 添加/修改 拼团活动---对话框 -->
    <div>
      <el-dialog v-model="dialogFormVisible" :title="title" width="500">
        <el-form :model="form">
          <el-form-item label="活动名称" :label-width="formLabelWidth">
            <el-input v-model="form.activeName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="参团人数" :label-width="formLabelWidth">
            <el-input-number v-model="form.groupNum" autocomplete="off" />
          </el-form-item>
          <el-form-item label="开始时间" :label-width="formLabelWidth">
            <el-date-picker
              v-model="form.beginTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="datetime"
              placeholder="请选择开始时间"
            />
          </el-form-item>
          <el-form-item label="截止时间" :label-width="formLabelWidth">
            <el-date-picker
              v-model="form.endTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="datetime"
              placeholder="请选择截止时间"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button
              type="primary"
              v-if="title == '添加拼团活动'"
              @click="onSumbitAdd"
            >
              提交
            </el-button>
            <el-button
              type="primary"
              v-if="title == '修改拼团活动'"
              @click="onSumbitUpdate"
            >
              完成
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import {
  getGroupActivePage,
  addGroupActive,
  updateGroupActive,
  deleteGroupActive,
} from "../api/api";
import { ElMessage, ElMessageBox } from "element-plus";

const tableData = ref([]);
const total = ref(0);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: "",
});

const title = ref("");
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const form = ref({
  activeName: "",
  beginTime: "",
  endTime: "",
  groupNum: 1,
});

//删除拼团活动
const onDeleteActive = (row: any) => {
  console.log("onDeleteActive", row);

  ElMessageBox.confirm("删除之后无法恢复，您确认要删除吗?", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then((res) => {
      deleteGroupActive(row.activeId).then((res) => {
        if (res.data.code == 200) {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          loadGroupActiveList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};

//显示添加拼团活动对话框
const onUpdateActive = (row: any) => {
  title.value = "修改拼团活动";
  Object.assign(form.value, row);
  dialogFormVisible.value = true;
};

//确认修改
const onSumbitUpdate = () => {
  console.log("onSumbitUpdate");
  updateGroupActive(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      loadGroupActiveList();
      dialogFormVisible.value = false;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

//显示添加拼团活动对话框
const toAddGroupActive = () => {
  title.value = "添加拼团活动";
  form.value = {
    activeName: "",
    beginTime: "",
    endTime: "",
    groupNum: 1,
  };
  dialogFormVisible.value = true;
};

//确认添加
const onSumbitAdd = () => {
  console.log("onSumbitAdd");
  addGroupActive(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      loadGroupActiveList();
      dialogFormVisible.value = false;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadGroupActiveList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadGroupActiveList();
};

const loadGroupActiveList = () => {
  getGroupActivePage(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  loadGroupActiveList();
});
</script>

<!-- 分页样式 -->
<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>
<!-- 条件搜索样式 -->
<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
